import axios from 'axios'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
export const URL = 'http://47.104.134.128:8080'
const http = axios.create({
  baseURL: URL,
  timeout: 5000,
})
http.interceptors.request.use(
  function (config) {
    // Do something before request is sent
    nprogress.start()
    config.headers.Authorization = sessionStorage.getItem('token')
    return config
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error)
  }
)

// Add a response interceptor
http.interceptors.response.use(
  function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    nprogress.done()
    return response.data
  },
  function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    nprogress.done()
    return Promise.reject(error)
  }
)

/**
 * 发起一个get请求
 * @param {*} url
 * @param {*} params
 * @returns
 */
export const get = (url, params) =>
  http.get(url, {
    params, //url中传递的参数
  })

/**
 * 发起post请求
 * @param {*} url
 * @param {*} data
 * @returns
 */
export const post = (url, data) => http.post(url, data)

/**
 * 发起put请求
 * @param {*} url
 * @param {*} data
 * @returns
 */
export const put = (url, data) => http.put(url, data)

/**
 * 发起delete请求
 * @param {*} url
 * @returns
 */
export const del = (url) => http.delete(url)

export default http
